package com.mytrip.webapp.interceptor;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.mytrip.base.exception.BaseException;
import com.mytrip.webapp.exception.ActionExceptionWraper;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class BaseExceptionHandler extends AbstractInterceptor{
	
	private final Log log = LogFactory.getLog(BaseExceptionHandler.class);

	private static final long serialVersionUID = -67169849203862764L;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		try{
			return invocation.invoke();
		} catch (BaseException e){
			ActionExceptionWraper aw = new ActionExceptionWraper();
			aw.addActionError("[" + e.getErrCode() + "]:" + e.getMessage());
			
			invocation.getStack().push(aw);
			return "baseError";
		} catch (Exception e){
			log.error("::",e);
			ActionExceptionWraper aw = new ActionExceptionWraper();
			aw.addActionError("[unknow error]:" + e.getMessage()==null?null:e.getMessage().replaceAll("\n", "<br>"));
			
			invocation.getStack().push(aw);
			return "baseError";
		}
		
	}
	
}
